package 一些基础的数据结构;


public class DeleteGivenVaule {
    /**
     * 删除单链表中所有给定值的节点
     * @param head
     * @param num
     * @return
     */
    public static Node removeValue(Node head,int num){
        // head来到第一个不需要删的位置
        while(head != null){
            if (head.value != num){
                break;
            }
            head = head.next;
        }

        // 1)head == null
        // 2)head != null
        Node cur = head;
        Node pre = head;
        while (cur != null){
            if (cur.value == num){
                pre.next = cur.next;
            }else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}
